package com.js.nickcar.api;


import com.js.nickcar.entity.remote.LoginBean;

import java.util.ArrayList;

/**
 * Created by 李宗源 on 2017/3/7.
 */

public class UserObserver {

    private static ArrayList<UserSubscribe> lists = new ArrayList<>();

    private UserObserver() {
    }

    public static synchronized UserObserver getInstance() {
        return UserObserverHolder.instance;
    }

    public static void register(UserSubscribe subscribe) {
        if (subscribe != null && !lists.contains(subscribe)) {
            lists.add(subscribe);
        }
    }

    public static void unRegister(UserSubscribe subscribe) {
        if (subscribe != null && lists.contains(subscribe)) {
            lists.remove(subscribe);
        }
    }

    public static void notifyPersonInfoChanged() {
        for (UserSubscribe subscribe : lists) {
            subscribe.onPersonInfoChange(UserHelper.getLoginInfo());
        }
    }

    public static void clear() {
        lists.clear();
    }

    public interface UserSubscribe {
        void onPersonInfoChange(LoginBean loginBean);
    }

    private static class UserObserverHolder {
        private final static UserObserver instance = new UserObserver();
    }
}
